//
//  CommonTFEditViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/3.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "CommonTFEditViewController.h"

@interface CommonTFEditViewController ()

@property (nonatomic, strong) UIButton *saveBtn;

@end

@implementation CommonTFEditViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldEditChanged:)
              name:@"UITextFieldTextDidChangeNotification" object:self.editTF];
    
    self.view.backgroundColor = UIColorHex(0xffffff);
    self.saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.saveBtn setTitleColor:UIColorHex(0xff4242) forState:UIControlStateNormal];
    [self.saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [self.saveBtn addTarget:self action:@selector(handleAction) forControlEvents:UIControlEventTouchUpInside];
    self.saveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.saveBtn.frame = CGRectMake(0,0,40,30);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.saveBtn];

    self.navigationItem.rightBarButtonItems =@[rightItem];

    self.editTF.placeholder = self.placeholderStr;
    if (self.contentStr.length > 0) {
        self.editTF.text = self.contentStr;
//        self.countLab.text = [NSString stringWithFormat:@"%ld/50",self.contentStr.length];
    }
    
    [self.editTF becomeFirstResponder];
    
    if (_limitedCount > 0) {
        self.countLab.hidden = NO;
    } else {
        self.countLab.hidden = YES;
    }
    
    [[RACSignal merge:@[self.editTF.rac_textSignal, RACObserve(self.editTF, text)]] subscribeNext:^(NSString* text){
        
        NSLog(@"%@",self.editTF.text);
        if (self.editTF.text.length > 0) {
            [self.saveBtn setTitleColor:UIColorHex(0xff4242) forState:UIControlStateNormal];
        } else {
            [self.saveBtn setTitleColor:UIColorHex(0x666666) forState:UIControlStateNormal];
        }
        self.countLab.text = [NSString stringWithFormat:@"%ld/50",self.editTF.text.length];
    }];
}

-(void)textFieldEditChanged:(NSNotification *)obj {
    if (_limitedCount > 0) {
        UITextField *textField = (UITextField *)obj.object;
        NSString *toBeString = textField.text;
        
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > _limitedCount) {
                NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:_limitedCount];
                if (rangeIndex.length == 1) {
                    textField.text = [toBeString substringToIndex:_limitedCount];
                } else {
                    NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, _limitedCount)];
                    textField.text = [toBeString substringWithRange:rangeRange];
                }
            }
        }
    }
}

- (void)handleAction {
    if (self.editTF.text.length > 0) {
        if (self.completion) {
            self.completion(self.editTF.text);
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    } else {
        [SVProgressHUD showErrorWithStatus:self.placeholderStr];
        [SVProgressHUD dismissWithDelay:1];
    }
}

@end
